package com.xu.aspect;

import com.xu.common.utils.ServletUtils;
import com.xu.modules.service.VisitorService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * ip 地址切面
 *
 * @author xu
 * 2022/7/27 17:16
 */
@Slf4j
@Aspect
@Component
public record IpAspect(VisitorService visitorService) {
    @Pointcut("execution(* com.xu.modules.controller.*.*(..))")
    public void controller() {

    }

    @Before("controller()")
    public void beforeController() {
        String remoteIP = ServletUtils.getRequest().getRemoteAddr();
        // 整理一下 把本地IP 改成localhost
        remoteIP = remoteIP.equals("0:0:0:0:0:0:0:1") ? "localhost" : remoteIP;
        visitorService.add(remoteIP);
    }
}
